package com.handinglian.ai.record.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.handinglian.ai.common.dto.CommonPage;
import com.handinglian.ai.common.util.FastJsonUtil;
import com.handinglian.ai.record.dto.MessageDto;
import com.handinglian.ai.record.entity.MessageRecord;
import com.handinglian.ai.record.mapper.MessageRecordMapper;
import com.handinglian.ai.record.param.MessageParam;
import com.handinglian.ai.record.service.MessageRecordService;
import com.handinglian.ai.thirdparty.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class MessageRecordServiceImpl implements MessageRecordService {
    @Autowired
    private MessageRecordMapper messageRecordMapper;
    @Autowired
    private MessageService messageService;

    @Override
    public CommonPage<MessageDto> listPage(String nameOrPhone, String startDate, String endDate, Integer pageNum, Integer pageSize) {
        Date sDate = StrUtil.isBlank(startDate)?null:DateUtil.parseDateTime(startDate+" 00:00:00");
        Date eDate = StrUtil.isBlank(endDate)?null:DateUtil.parseDateTime(endDate+" 23:59:59");
        PageHelper.startPage(pageNum,pageSize);
        List<MessageRecord> messageRecords = messageRecordMapper.list(nameOrPhone, sDate, eDate);
        return CommonPage.restPage(messageRecords, MessageDto.class);
    }

    @Override
    public MessageDto getDetail(Integer id) {
        MessageRecord messageRecord = messageRecordMapper.selectByPrimaryKey(id);
        return FastJsonUtil.ObjectToObject(messageRecord, MessageDto.class);
    }

    /*@Override
    public int sendSingleMessage( MessageParam messageParam) throws IOException {
        String content = messageParam.getContent();
        content = content.replaceAll("\\{姓名}",messageParam.getName());
        content = content.replaceAll("\\{预约时间}", messageParam.getAppointmentTime());
        messageParam.setContent(content);
        yunqixunApiService.singleSend(messageParam.getMobilePhone(), content);

        MessageRecord messageRecord = convertToMessageRecord(messageParam);
        return create(messageRecord);
    }

    private MessageRecord convertToMessageRecord(MessageParam messageParam) {
        MessageRecord messageRecord = new MessageRecord();
        messageRecord.setMassName(messageParam.getName());
        messageRecord.setMassMobilePhone(messageParam.getMobilePhone());
        messageRecord.setContent(messageParam.getContent());
        messageRecord.setCreateTime(new Date());
        return messageRecord;
    }*/

    @Override
    @Transactional
    public int create(MessageRecord record) {
        return messageRecordMapper.insertSelective(record);
    }

    @Override
    public int sendMessage(MessageParam messageParam) throws IOException {
        messageService.sendMessage(messageParam.getMobilePhones(), messageParam.getContent());
        return 0;
    }
}
